/*
 * @lc app=leetcode.cn id=169 lang=typescript
 *
 * [169] 多数元素
 */

// @lc code=start
function majorityElement(nums: number[]): number {
    const size = nums.length / 2;
    const map = new Map();
    let res = nums[0];
    for (let i = 0; i < nums.length; i++) {
        const len = map.get(nums[i])
        if (len) {
            if (len + 1 > size) {
                res = nums[i];
                break;
            }
            map.set(nums[i], len + 1)
        } else {
            map.set(nums[i], 1)
        }
    }
    return res;
};

// majorityElement([2, 2, 1, 1, 1, 2, 2])
// @lc code=end

